package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectTypeDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectType;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectTypeService {
    private static GraduateProjectTypeDao graduateProjectTypeDao =
            GraduateProjectTypeDao.getInstance();
    private static GraduateProjectTypeService graduateProjectTypeService =
            new GraduateProjectTypeService();

    public static GraduateProjectTypeService getInstance() {
        return graduateProjectTypeService;
    }


    public GraduateProjectType find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        GraduateProjectType result = graduateProjectTypeDao.find(id, connection);
        JdbcHelper.close(connection);
        return result;
    }

    public Collection<GraduateProjectType> findAll()
            throws SQLException {

        Connection connection = JdbcHelper.getConn();
        Collection<GraduateProjectType> graduateProjectTypes =
                graduateProjectTypeDao.findAll(connection);
        JdbcHelper.close(connection);
        return graduateProjectTypes;
    }

    public boolean update(GraduateProjectType graduateProjectType) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        connection.setAutoCommit(false);
        try {
            updated = graduateProjectTypeDao.update(graduateProjectType, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(GraduateProjectType graduateProjectType) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = graduateProjectTypeDao.add(graduateProjectType, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;

    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        GraduateProjectType graduateProjectType = this.find(id);
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = graduateProjectTypeDao.delete(graduateProjectType, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(GraduateProjectType graduateProjectType_json, Connection connection) throws SQLException, BysjException {
        boolean result = false;
        int countTeacherOfProfTitle = CommonDao.getInstance().
                countWhereForeignKey(connection, "graduateProject", "graduateProjectType_id", graduateProjectType_json.getId());
        //若没有二级单位，则能够删除
        if (countTeacherOfProfTitle == 0) {
            result = graduateProjectTypeDao.delete(graduateProjectType_json, connection);
        } else {
            throw new BysjException(graduateProjectType_json.getDescription() + "有下属系，不能删除");
        }
        JdbcHelper.close(connection);
        return result;
    }

}
